package com.yannickstucki.android.musicqueue.widget;

import android.app.PendingIntent;
import android.appwidget.AppWidgetManager;
import android.appwidget.AppWidgetProvider;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.widget.RemoteViews;

import com.yannickstucki.android.musicqueue.R;
import com.yannickstucki.android.musicqueue.data.DbAdapter;

/**
 * Class that takes care of the Widget behavior.
 */
public final class Widget extends AppWidgetProvider {

  @Override
  public void onEnabled(Context context) {
    // TODO Auto-generated method stub
    super.onEnabled(context);
  }

  @Override
  public void onUpdate(Context context, AppWidgetManager appWidgetManager,
      int[] appWidgetIds) {
    Widget.onUpdate(context, appWidgetManager);
  }

  public static void onUpdate(Context context, AppWidgetManager appWidgetManager) {
    final RemoteViews rv = new RemoteViews(context.getPackageName(),
        R.layout.widget);
  /* rv.setOnClickPendingIntent(R.id.widgetLogo, PendingIntent.getActivity(
        context, 0, new Intent(context, MusicQueue.class),
        Intent.FLAG_ACTIVITY_NEW_TASK));*/
    rv.setOnClickPendingIntent(R.id.widgetPlay, PendingIntent.getBroadcast(
        context, 1, new Intent(context, WidgetChange.class).putExtra(
            DbAdapter.KEY_ROWID, R.id.widgetPlay), 0));
    rv.setOnClickPendingIntent(R.id.widgetNext, PendingIntent.getBroadcast(
        context, 2, new Intent(context, WidgetChange.class).putExtra(
            DbAdapter.KEY_ROWID, R.id.widgetNext), 0));
    WidgetChange.refresh(rv, context);
    appWidgetManager.updateAppWidget(new ComponentName(context, Widget.class),
        rv);
  }
}
